package com.hmall.user.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component //注意当前类必须受Spring容器控制
@Slf4j
public class MyInterceptor implements HandlerInterceptor {
    //处理器运行之前执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String header = request.getHeader("authorization");
        if ("2".equals(header)){
            ThreadLocalUtil.setCurrentId(Long.valueOf(header));
            return true;
        }
        return false;
    }



    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response,
                                Object handler,
                                Exception ex) throws InterruptedException {

        Long currentId = ThreadLocalUtil.getCurrentId();
        log.warn("当前线程里的值id是:{}",currentId);

        ThreadLocalUtil.remove();
    }
}